package utils

import (
	"HeadLineNews/pkg/zaplog"
	"HeadLineNews/resp"
	"HeadLineNews/verify"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/validator/v10"
)

func ShouldBindProtobuf(c *gin.Context, p interface{}) (err error) {
	if err = c.ShouldBindWith(p, binding.ProtoBuf); err != nil {
		zaplog.CallerL().Error("invalid request format", zaplog.Fields(c, nil, nil)...)
		resp.ServerBusy(c)
		return
	}
	if err = verify.V().Struct(p); err != nil {
		if vErr, ok := err.(validator.ValidationErrors); ok {
			errs := verify.GetValidError(vErr)
			zaplog.CallerL().Error("parse with invalid params", zaplog.Fields(c, nil, errs)...)
			resp.InvalidParam(c, errs)
			return
		}
		zaplog.CallerL().Error("unmarshal params failed", zaplog.Fields(c, nil, err)...)
		resp.ServerBusy(c)
		return
	}
	return
}


